﻿using Digitalmes.Infrastructure.Brokers;
using Digitalmes.Infrastructure.Data;
using Yitter.IdGenerator;

namespace Digitalmes.Infrastructure;

public static class DependencyInjection
{
    /// <summary>
    /// 注册基础设施服务。
    /// </summary>
    public static void AddInfrastructureSetup(this IHostBuilder builder)
    {
        // 雪花Id
        YitIdHelper.SetIdGenerator(new IdGeneratorOptions
        {
            WorkerId = 1, // 机器码 全局唯一
            WorkerIdBitLength = 1, // 机器码位长 默认值6，取值范围 [1, 19]
            SeqBitLength = 6, // 序列数位长 默认值6，取值范围 [3, 21]（建议不小于4，值越大性能越高、Id位数也更长）
        });

        builder.ConfigureServices((hostBuilder, services) =>
        {
            services.AddMemoryCache();
            services.AddSingleton(TimeProvider.System);

            services.AddSingleton(typeof(IMessageQueue<>), typeof(DefaultChannelQueue<>));
            services.AddSqlSugarSetup(hostBuilder);
        });
    }
}
